package com.ruoyi.oa.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.oa.domain.OaProject;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.OaPurchaesPayment;
import com.ruoyi.oa.domain.vo.OaPurchaesPaymentVo;
import com.ruoyi.oa.domain.bo.OaPurchaesPaymentBo;
import com.ruoyi.oa.service.IOaPurchaesPaymentService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 付款申请
 *
 * @author ruoyi
 * @date 2023-04-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/purchaesPayment")
public class OaPurchaesPaymentController extends BaseController {

    private final IOaPurchaesPaymentService iOaPurchaesPaymentService;

    /**
     * 查询付款申请列表
     */
//    @SaCheckPermission("oa:purchaesPayment:list")
    @GetMapping("/list")
    public TableDataInfo<OaPurchaesPaymentVo> list(OaPurchaesPaymentBo bo, PageQuery pageQuery) {
        return iOaPurchaesPaymentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出付款申请列表
     */
//    @SaCheckPermission("oa:purchaesPayment:export")
    @Log(title = "付款申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaPurchaesPaymentBo bo, HttpServletResponse response) {
        List<OaPurchaesPaymentVo> list = iOaPurchaesPaymentService.queryList(bo);
        ExcelUtil.exportExcel(list, "付款申请", OaPurchaesPaymentVo.class, response);
    }

    /**
     * 获取付款申请详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("oa:purchaesPayment:query")
    @GetMapping("/{id}")
    public R<OaPurchaesPaymentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String id) {
        return R.ok(iOaPurchaesPaymentService.queryById(id));
    }

    @RequestMapping("/queryByProjectName")
    public R<OaPurchaesPaymentVo> queryByProjectName(@RequestBody Map<String,Object> map) {
        return R.ok(iOaPurchaesPaymentService.queryByProjectName(map.get("projectName").toString()));
    }

    /**
     * 新增付款申请
     */
//    @SaCheckPermission("oa:purchaesPayment:add")
    @Log(title = "付款申请", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<OaPurchaesPaymentBo> add(@Validated(AddGroup.class) @RequestBody OaPurchaesPaymentBo bo) {
        OaPurchaesPaymentVo oaPurchaesPaymentVo = iOaPurchaesPaymentService.queryByProjectName(bo.getProjectName());
        if (ObjectUtil.isNotEmpty(oaPurchaesPaymentVo)){
            return R.fail(bo.getProjectName()+"已经存在,不能重复申请.");
        }
        return R.ok(iOaPurchaesPaymentService.insertByBo(bo));
    }

    /**
     * 修改付款申请
     */
//    @SaCheckPermission("oa:purchaesPayment:edit")
    @Log(title = "付款申请", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<OaPurchaesPayment> edit(@Validated(EditGroup.class) @RequestBody OaPurchaesPaymentBo bo) {
        return R.ok(iOaPurchaesPaymentService.updateByBo(bo));
    }

    /**
     * 删除付款申请
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("oa:purchaesPayment:remove")
    @Log(title = "付款申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(iOaPurchaesPaymentService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @Log(title = "获取没有合同申请的项目信息")
    @GetMapping("/getProjectList")
    public R<List<OaProject>> getProjectList() {
        return R.ok(iOaPurchaesPaymentService.getProjectList());
    }

}
